package com.li.utlis;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.li.domain.SystemAddress;
import com.li.service.SystemAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 文件地址工具类
 */
public class FilePathUtil {

    @Autowired
    private SystemAddressService systemAddressService;

    /**
     * 物理地址转为访问地址
     * @param path
     * @param req
     * @return
     */
    public static String pathToUrl(String path , HttpServletRequest req){
        String url=req.getScheme() + "://" + req.getServerName() + ":" +
                req.getServerPort() + path.split(":")[1];
        return url;
    }

    /**
     * 判断路径最后一位是否有 "\"  或 "/" 没有的话就给补上
     * @param path
     * @return
     */
    public static String pathLast(String path){
        String lastSysPath=path.substring(path.length()-1);
        if (!"/".equals(lastSysPath)){
            if (!"\\".equals(lastSysPath)){
                path+="/";
            }
        }
        return path;
    }

    /**
     * 根据type获取不同类型的路径，
     * @param systemAddressService
     * @param type
     * @return
     */
    public static String getSystemAddressServicePath(SystemAddressService systemAddressService,String type){
        String path="";
        List<SystemAddress> list = systemAddressService.list(new QueryWrapper<SystemAddress>().eq("type", type));
        if (CommonUtil.listIsNull(list)){
            path=list.get(0).getSysPath();
        }
        return path;
    }
}
